package com.deyuanyun.pic.pipe.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.caixu.beanvalidateutil.imlclass.BeanValidateUtil;
import com.caixu.beanvalidateutil.imlclass.VResult;
import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.dto.DTOResult;
import com.deyuanyun.pic.common.exception.ExceptionCode;
import com.deyuanyun.pic.common.exception.ExceptionCodeUtil;
import com.deyuanyun.pic.common.exception.bizcode.basedata.pipe.PipeElbowCodeEnum;
import com.deyuanyun.pic.common.exception.code.ModuleMsg;
import com.deyuanyun.pic.common.tool.JXL;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.domain.base.UnifiedEncoding;
import com.deyuanyun.pic.domain.pipe.PipeSegment;
import com.deyuanyun.pic.mapper.base.UnifiedEncodingMapper;
import com.deyuanyun.pic.pipe.domain.exceldomain.CelbowImportDemo;
import com.deyuanyun.pic.pipe.domain.exceldomain.ElbowImportDemo;
import com.deyuanyun.pic.pipe.mapper.CelbowMapper;
import com.deyuanyun.pic.pipe.mapper.DyyPipeLineMapper;
import com.deyuanyun.pic.pipe.mapper.ElbowMapper;
import com.deyuanyun.pic.pipe.service.PipeElbowService;
import com.deyuanyun.pic.pipe.support.utils.UniformCodeUtils;
import com.deyuanyun.pic.service.pipe.PipeSegmentService;
import com.deyuanyun.pic.support.biz.CodeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @ClassName PipeElbowServiceImpl
 * @Description 管道基础数据 ->弯头service实现
 * @author 王敏
 * @date 2015年1月18日
 */
@Service
public class PipeElbowServiceImpl implements PipeElbowService {
	@Resource
	private PipeSegmentService pipeSegmentService;
	
	@Resource
	private ElbowMapper elbowMapper;

	@Resource
	private CelbowMapper celbowMapper;
	

	@Resource
	private UnifiedEncodingMapper unifiedEncodingMapper;
	
	@Resource
	private DyyPipeLineMapper dyyPipeLineMapper;
	

	public List<List<String[]>> getDownloadElbow(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage, BigDecimal endMileage) {

		// 设置一个返回值
		List<List<String[]>> result = new ArrayList<>();

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (mileageEnd != null && !"".equals(mileageEnd)) {
			mileageEnd = endMileage;
		}

		// 定义一个管段ID
		String pipeSegmentIdStr = "";

		if (!"all".equals(pipeSegmentId)) {
			pipeSegmentIdStr = pipeSegmentId;
		}

		// 热弯头List
		List<LinkedHashMap<String, Object>> hotElbowMap = elbowMapper
				.downloadElbow(pipeLineId, pipeSegmentIdStr, startMileage,
						endMileage);
		ArrayList<LinkedHashMap<String, Object>> hotElbowMap2 = new ArrayList<LinkedHashMap<String, Object>>();
		for (int i = 0; i < hotElbowMap.size(); i++) {
			LinkedHashMap<String, Object> map = hotElbowMap.get(i);
			map.put("MATERIAL", CodeUtil.getName((String) map.get("MATERIAL")));
			map.put("ANTISEPTICMATERIAL",
					CodeUtil.getName((String) map.get("ANTISEPTICMATERIAL")));
			map.put("ELBOWTYPE",
					CodeUtil.getName((String) map.get("ELBOWTYPE")));
			map.put("OUTLETCONNECTIONTYPE",
					CodeUtil.getName((String) map.get("OUTLETCONNECTIONTYPE")));
			hotElbowMap2.add(map);
		}
		if (hotElbowMap2.size() == 0) {
			hotElbowMap2 = null;
		}
		result.add(JXL.toList(hotElbowMap2, "yyyy-MM-dd HH:mm:ss"));

		// 冷弯头List
		List<LinkedHashMap<String, Object>> celbowMap = celbowMapper
				.selectCelbowtoExcel(pipeLineId, pipeSegmentIdStr,
						mileageStart, mileageEnd);
		ArrayList<LinkedHashMap<String, Object>> celbowMap2 = new ArrayList<LinkedHashMap<String, Object>>();
		for (int i = 0; i < celbowMap.size(); i++) {
			LinkedHashMap<String, Object> map = celbowMap.get(i);
			map.put("COATINGM_ATERIAL",
					CodeUtil.getName((String) map.get("COATINGM_ATERIAL")));
			celbowMap2.add(map);
		}
		if (celbowMap2.size() == 0) {
			celbowMap2 = null;
		}
		result.add(JXL.toList(celbowMap2, "yyyy-MM-dd HH:mm:ss"));

		return result;
	}

	@Override
	public DTOResult getDetailedByIdAndSource(String elbowId, Integer source) {

		// 判断主键ID是否为NULL
		if (StringUtils.isNull(elbowId) == true) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeElbowCodeEnum.elbowIdIsNull.getCode(),
					PipeElbowCodeEnum.elbowIdIsNull.getLable());
		}

		// 判断类型是否为Null
		if (null == source) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeElbowCodeEnum.sourceIsNull.getCode(),
					PipeElbowCodeEnum.sourceIsNull.getLable());
		}

		// 类型大于1
		if (source > 1) {
			return new DTOResult(
					ExceptionCodeUtil.getExceptionCode(ModuleMsg.BASE_DATA)
							+ PipeElbowCodeEnum.sourceOutofRange.getCode(),
					PipeElbowCodeEnum.sourceOutofRange.getLable());
		}

		HashMap<String, Object> map = null;

		// 判断source来源
		if (0 == source) {
			// 如果来源是0，则说明查询的是热煨弯头详情
			map = elbowMapper.getElbowDetailed(elbowId);
			map.put("MATERIAL", CodeUtil.getName((String) map.get("MATERIAL")));
			map.put("ANTISEPTIC_MATERIAL",
					CodeUtil.getName((String) map.get("ANTISEPTIC_MATERIAL")));
			map.put("ELBOW_TYPE",
					CodeUtil.getName((String) map.get("ELBOW_TYPE")));
			map.put("OUTLET_CONNECTION_TYPE",
					CodeUtil.getName((String) map.get("OUTLET_CONNECTION_TYPE")));
			
			
			
		}

		if (1 == source) {
			// 如果来源是1，则说明查询的是冷弯头详情
			map = celbowMapper.getCelbowDetailedById(elbowId);
			if (null!=map &&"".equals(map)){
				map.put("COATINGM_ATERIAL", CodeUtil.getName((String) map.get("COATINGM_ATERIAL")));
			}
		}

		return new DTOResult(ExceptionCode.SUCCESS.getCode(),
				ExceptionCode.SUCCESS.getLabel(), map);
	}

	@Override
	public Page<HashMap<String, Object>> selectElbowBase(String pipeLineId,
			String pipeSegmentId, BigDecimal startMileage,
			BigDecimal endMileage, Integer pageNum, Integer pageSize) {

		int page = 1;

		int size = 10;
		// 初始化当前页数
		if (pageNum != null) {
			page = pageNum;
		}
		// 初始化每页显示数量
		if (pageSize != null) {
			size = pageSize;
		}

		System.err.println("当前页数为:" + page + "每页显示数量为:" + size);

		PageHelper.startPage(page, size);

		// 定义一个里程起止点
		BigDecimal mileageStart = new BigDecimal(0);

		// 定义一个里程终点
		BigDecimal mileageEnd = new BigDecimal(0);

		// 如果传入的起止点不为NULL或 ""
		if (startMileage != null) {
			mileageStart = startMileage;
		}

		// 如果传入的终点不为NULL或""
		if (mileageEnd != null && !"0".equals(mileageEnd)) {
			mileageEnd = endMileage;
		}

		return elbowMapper.selectElbowBaseByParam(pipeLineId, pipeSegmentId,
				mileageStart, mileageEnd);
	}

	

	@Override
	public boolean importElbowData(List<ElbowImportDemo> data) throws Exception{
		Map<String, Object> bean = null;
		data=UniformCodeUtils.sortedByLine(data,"pipeLineId","","code","热煨弯头");
		
	
		
		
		for(ElbowImportDemo info:data)
	    {
			
			String valueId=UUID.randomUUID().toString().replaceAll("-", "");
			bean=MapUtil.beanToMap(info);
			bean.put("id", valueId);
    		elbowMapper.insertData(bean);
    		elbowMapper.insertPositData(bean);
	    }
		return false;
	}

	@Override
	public boolean importCElbowData(List<CelbowImportDemo> data) throws Exception{
		
		data=UniformCodeUtils.sortedByLine(data,"pipeLineId","","code","冷弯弯头");
		
		
		
		Map<String, Object> bean = null;
    	for(CelbowImportDemo info:data)
	    {
    		
    	
    		
    		String valueId=UUID.randomUUID().toString().replaceAll("-", "");
			bean=MapUtil.beanToMap(info);
			bean.put("id", valueId);
    		celbowMapper.insertData(bean);
    		celbowMapper.insertPositData(bean);
    		
	    }
		return false;
	}

	@Override
	public Page<HashMap<String, Object>> findPage(HashMap param) {
		Page<HashMap<String, Object>> page=elbowMapper.findPage(param);
		for(HashMap<String, Object> m:page)
		{
			m.put("MATERIAL", CodeUtil.getName((String)m.get("MATERIAL")));
			m.put("ANTISEPTIC_MATERIAL", CodeUtil.getName((String)m.get("ANTISEPTIC_MATERIAL")));
			m.put("ELBOW_TYPE", CodeUtil.getName((String)m.get("ELBOW_TYPE")));
			m.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("OUTLET_CONNECTION_TYPE")));
			m.put("COATINGM_ATERIAL", CodeUtil.getName((String)m.get("COATINGM_ATERIAL")));
			
		}
		MapUtil.emptyConvert(page);
		return page;
	}

	public int delElbowGis(Map map){
		return elbowMapper.delGis(map);
	}
	public int delElbowData(Map map){
		elbowMapper.delGis(map);
		elbowMapper.delData(map);
		return 1;
	}
	
	public int delCElbowGis(Map map){
		return celbowMapper.delGis(map);
	}
	public int delCElbowData(Map map){
		celbowMapper.delData(map);
		celbowMapper.delGis(map);
		return 1;
	}

	@Override
	public List<List<String[]>> excelExport(Map map) {
		List<LinkedHashMap<String, Object>> data = elbowMapper.exportExcel(map);
		for(HashMap<String, Object> m:data)
		{
			m.put("MATERIAL", CodeUtil.getName((String)m.get("MATERIAL")));
			m.put("ANTISEPTIC_MATERIAL", CodeUtil.getName((String)m.get("ANTISEPTIC_MATERIAL")));
			m.put("ELBOW_TYPE", CodeUtil.getName((String)m.get("ELBOW_TYPE")));
			m.put("OUTLET_CONNECTION_TYPE", CodeUtil.getName((String)m.get("OUTLET_CONNECTION_TYPE")));
			
		}
		MapUtil.emptyConvertLink(data);
		List<List<String[]>> result=new ArrayList<List<String[]>>();
		List<String[]> lsr = JXL.toList(data, "yyyy-MM-dd HH:mm:ss");
		result.add(lsr);
		data = celbowMapper.exportExcel(map);
		for(HashMap<String, Object> m:data)
		{
			m.put("COATINGM_ATERIAL", CodeUtil.getName((String)m.get("COATINGM_ATERIAL")));
			
		}
		MapUtil.emptyConvertLink(data);
		lsr = JXL.toList(data, "yyyy-MM-dd HH:mm:ss");
		result.add(lsr);
		
		return result;
	}
}
